var stompClient = null;

function connect() {
    var socket = new SockJS('/chat');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        stompClient.subscribe('/user/queue/chat', function (chat) {
            showGreeting(JSON.stringify(chat.body)); // 会转成json字符串""
            //showGreeting(JSON.parse(chat.body));
        });
    });
}

function showGreeting(message) {
    var obj = eval('(' + message + ')');
    var obj1 = eval('(' + obj + ')');
    $("#chatsContent")
        .append("<div>" + obj1.from + ":" + obj1.content + "</div>")
}

function sendMSG() {
    stompClient.send("/app/chat", {}, JSON.stringify({
        'content': $("#content").val(),
        'to': $("#to").val()
    }));
}

$(function () {
    connect();
    $("#send").click(function () {
        sendMSG();
    })
})